package com.lifeng.mynetty.client.handler;

import com.lifeng.mynetty.constants.MyNettyContants;
import com.lifeng.mynetty.response.Cache;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.AttributeKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.charset.Charset;

/**
 * Created by lifeng
 * 2017/9/1 15:55
 */
public class TcpClientHandler extends ChannelInboundHandlerAdapter{
    private static Logger logger = LoggerFactory.getLogger(TcpClientHandler.class);
    private String charset;
    public TcpClientHandler(){}
    public TcpClientHandler(String charset){
        this.charset = charset;
    }

    public void channelRead(ChannelHandlerContext channelHandlerContext, Object o) throws Exception {
        logger.info("client接收到服务器返回的消息:"+o);
        String att = (String)channelHandlerContext.channel().attr(AttributeKey.valueOf(MyNettyContants.MSG_KEY)).get();
        if(att != null && !"".equals(att)){
            logger.debug("附件属性:"+att);
            Cache.getInstance().getCallback(att).callback(o);
        }
    }
}
